package com.zhijian.medical.service.impl;

import com.zhijian.entity.PageVo;
import com.zhijian.medical.dao.local.AgentiaMapper;
import com.zhijian.medical.entity.po.Agentia;
import com.zhijian.medical.entity.po.AgentiaCategory;
import com.zhijian.medical.entity.vo.request.AgentiaSearchBmVo;
import com.zhijian.medical.entity.vo.request.AgentiaSearchVo;
import com.zhijian.medical.entity.vo.response.AgentiaBmVo;
import com.zhijian.medical.entity.vo.response.AgentiaSimpleVo;
import com.zhijian.medical.entity.vo.response.AgentiaVo;
import com.zhijian.medical.enums.ResponseCodeEnum;
import com.zhijian.medical.exception.BusinessException;
import com.zhijian.medical.service.AgentiaCategoryService;
import com.zhijian.medical.service.AgentiaService;
import com.zhijian.medical.service.RoleService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 试剂表业务处理层
 *
 * @author HCG
 * @date 2022-01-13 15:33:49
 * @version: 1.0
 */
@Slf4j
@Service
public class AgentiaServiceImpl implements AgentiaService {

    @Autowired
    private AgentiaMapper mapper;

    @Autowired
    private RoleService roleService;

    @Autowired
    private AgentiaCategoryService agentiaCategoryService;

    @Override
    public Agentia selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public AgentiaVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public List<AgentiaBmVo>selectVoByCategoryId(AgentiaSearchBmVo bmVo) {
        return mapper.selectVoByCategoryId(bmVo);
    }

    @Override
    public int countByCategoryId(Long categoryId) {
        return mapper.countByCategoryId(categoryId);
    }

    @Override
    public AgentiaVo selectVoById(List<Long> roleIds, Long id) {
        AgentiaVo agentiaVo = this.selectVoById(id);
        if (roleService.isJxOrSxOrFz(roleIds)) {
            //如果是外部人员，则把价格置为null
            agentiaVo.setPrice(null);
        }
        return agentiaVo;
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(Agentia agentia) {
        Date now = new Date();
        agentia.setCreateTime(now);
        agentia.setUpdateTime(now);
        return mapper.insert(agentia) == 1 ? agentia.getId() : 0L;
    }

    @Override
    public boolean update(Agentia agentia) {
        Date now = new Date();
        agentia.setUpdateTime(now);
        return mapper.updateByPrimaryKey(agentia) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(Agentia record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(AgentiaSearchVo agentiaSearchVo) {
        return mapper.count(agentiaSearchVo);
    }

    @Override
    public List<AgentiaVo> selectList(AgentiaSearchVo agentiaSearchVo) {
        return mapper.selectList(agentiaSearchVo);
    }

    @Override
    public Page<AgentiaVo> selectPage(AgentiaSearchVo agentiaSearchVo) {
//        if (!CollectionUtils.isEmpty(agentiaSearchVo.getGroupIds())) {
//            //如果查询条件有部门id集合，挨个查出他们所有的子级部门，拼接查询所有的部门id
//            List<Long> groupIds = new LinkedList<>();
//            agentiaSearchVo.getGroupIds().forEach(groupId -> {
//                List<Long> newGroupIds = officeGroupService.concatIdsByParentId(groupId);
//                groupIds.addAll(newGroupIds);
//            });
//            agentiaSearchVo.setGroupIds(groupIds);
//        } else {
//            agentiaSearchVo.setGroupIds(Collections.singletonList(0L));
//        }
        //先直接查总数
        int count = count(agentiaSearchVo);
        //分页
        Page<AgentiaVo> page = new Page<>(true, count, agentiaSearchVo.getPageNum(), agentiaSearchVo.getPageSize());
        agentiaSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<AgentiaVo> responseVos = this.selectList(agentiaSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public Page<AgentiaBmVo> lakuaiInfo(AgentiaSearchBmVo agentiaSearchVo) {
        // todo： 这里写死石蜡的类型
        agentiaSearchVo.setCategoryId(40L);
        //先直接查总数
        int count = countByCategoryId(agentiaSearchVo.getCategoryId());
        //分页
        Page<AgentiaBmVo> page = new Page<>(true, count, agentiaSearchVo.getPageNum(), agentiaSearchVo.getPageSize());
        agentiaSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<AgentiaBmVo> responseVos = this.selectVoByCategoryId(agentiaSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean delivery(Long id, Integer count) {
        Agentia agentia = this.selectById(id);
        if (Objects.isNull(agentia)) {
            throw new BusinessException(ResponseCodeEnum.FAILURE.code, "此批次试剂不存在或者已被删除");
        }
        //库存
        Integer stock = agentia.getStock();
        if (stock < count) {
            throw new BusinessException(ResponseCodeEnum.FAILURE.code, "此批次试剂库存不足");
        }
        int i = mapper.updateByPrimaryKeySelective(Agentia.builder()
                .id(agentia.getId())
                .stock(stock - count)
                .updateTime(new Date())
                .build());
        return i > 0;
    }

    @Override
    public boolean checkStock(Long id, Integer stock) {
        Agentia agentia = this.selectById(id);
        if (Objects.isNull(agentia)) {
            throw new BusinessException(ResponseCodeEnum.FAILURE.code, "此批次试剂不存在或者已被删除");
        }

        if (stock < 0) {
            throw new BusinessException(ResponseCodeEnum.FAILURE.code, "库存数量不能小于0");
        }
        int i = mapper.updateByPrimaryKeySelective(Agentia.builder()
                .id(agentia.getId())
                .stock(stock)
                .updateTime(new Date())
                .build());
        return i > 0;
    }

    @Override
    public AgentiaSimpleVo selectAgentiaSimpleVo(Long agentiaId) {
        Agentia agentia = this.selectById(agentiaId);
        if (Objects.isNull(agentia)) {
            return null;
        }
        AgentiaSimpleVo agentiaSimpleVo = new AgentiaSimpleVo();
        agentiaSimpleVo.setName(agentia.getName());
        agentiaSimpleVo.setAgentiaId(agentiaId);
        agentiaSimpleVo.setBatchNumber(agentia.getBatchNumber());
        AgentiaCategory agentiaCategory = agentiaCategoryService.selectById(agentia.getCategoryId());
        if (Objects.nonNull(agentiaCategory)) {
            agentiaSimpleVo.setFactoryName(agentiaCategory.getFactory());
        }
        return agentiaSimpleVo;
    }

}